home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
Softshoe
/
Lisa's Mac Parts
/
Files
/
Locations
/
CopyFile.cp
next >
Wrap
Text File
|
2000-06-23
|
3KB
|
112 lines
// CopyFile.cp
#ifndef CopyFile_h
#include "CopyFile.h"
#endif
#ifndef FileLocation_h
#include "FileLocation.h"
#endif
#ifndef CatInfo_h
#include "CatInfo.h"
#endif
#ifndef FileReadingPath_h
#include "FileReadingPath.h"
#endif
#ifndef TentativeFile_h
#include "TentativeFile.h"
#endif
#ifndef FileWritingPath_h
#include "FileWritingPath.h"
#endif
#ifndef TemporaryMemory_h
#include "TemporaryMemory.h"
#endif
#ifndef AsyncFileWriter_h
#include "AsyncFileWriter.h"
#endif
#ifndef FileReadingAheadLoop_h
#include "FileReadingAheadLoop.h"
#endif
void CopyFile( const FileLocation& source, const FileLocation& destination )
{
Assert( source != destination );
Assert( source.NameIsValid() );
Assert( destination.NameIsValid() );
CopyFile( CatInfo( source ), destination );
}
void CopyFile( const CatInfo& source, const FileLocation& destination )
{
// First, make sure we can open the source file
FileReadingPath sourceData( source.Location(), FileReadingPath::dataFork );
FileReadingPath sourceResources( source.Location(), FileReadingPath::resourceFork );
// Create the destination
TentativeFile tentativeDestination( destination,
source.File().Type(),
source.File().Signature(),
source.Script() );
// Open the destination before anyone else gets much of a chance
FileWritingPath destinationData( destination,
FilePermission::ReadWrite(),
FileWritingPath::dataFork );
FileWritingPath destinationResources( destination,
FilePermission::ReadWrite(),
FileWritingPath::resourceFork );
// Copy the catalog info quickly, so other programs
// have less chance of seeing the wrong info.
{
CatInfo destinationInfo( source );
destinationInfo.BeUnseenByFinder();
destinationInfo.Set( destination );
}
// Allocate space to the copy
destinationData.Allocate( source.File().Data().LogicalLength() );
destinationResources.Allocate( source.File().Resources().LogicalLength() );
uint32 maxBufferSize = Max( source.File().Data().LogicalLength(),
source.File().Resources().LogicalLength() );
TemporaryMemory<512> bufferSpace( maxBufferSize );
// Copy the data fork
{
AsyncFileWriter writer( destinationData );
for ( FileReadingAheadLoop reader( bufferSpace, sourceData );
reader.Unfinished();
reader++ )
{
writer.LaunchWrite( *reader );
// There's dead time here until
// writer.ReadyToComplete() && reader.ReadyToAdvance()
writer.CompleteWrite();
}
}
// Copy the resource fork
{
AsyncFileWriter writer( destinationResources );
for ( FileReadingAheadLoop reader( bufferSpace, sourceResources );
reader.Unfinished();
reader++ )
{
writer.LaunchWrite( *reader );
// Again, dead time here.
writer.CompleteWrite();
}
}
destinationResources.Close();
destinationData.Close();
tentativeDestination.Commit();
sourceResources.Close();
sourceData.Close();
}